#include "PreHeader.h"
#include "DialogChellange.h"
#include "HBUtilies.h"
#include "LayerMain.h"
#include "DataSL.h"
#include "HBUmeng.h"

typedef enum
{
    kMenuLeft,
    kMenuRight,
    kMenuClose,
    kMenuStart
} MenuChellangeTag;

DialogChellange::DialogChellange()
{
}

DialogChellange::~DialogChellange()
{
}

DialogChellange* DialogChellange::create(CCNode* parent)
{
    DialogChellange *pRet = new DialogChellange;
    if(pRet && pRet->_initWithLevel(parent))
    {
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return NULL;
}

bool DialogChellange::_initWithLevel(CCNode* parent)
{
    CCSize s = CCDirector::sharedDirector()->getWinSize();
    
    CCLayerColor::initWithColor(ccc4(0,0,0,200), s.width, s.height);
	parent->addChild(this);
    
    float w2 = 160;

    _layerBase = CCLayer::create();
    _layerBase->setPosition(ccpRatio(w2,580));
    addChild(_layerBase);
    
    CCSprite* frame = createImageWithFrameName("dlg_bg.png", 0, 0, _layerBase);
    HBSetRatioScale(frame);
    
    createLabel(getLocalizedString("Level Select"), getLocalizedString("fontNormal"), 24, gAnchorCenter, ccWHITE, 0, 70, _layerBase);
    
    _curLevel = gTestLevel;
    
    _labelCurLevel = createLabelAtlas(fcs("%d", _curLevel + 1), "font_number.png", 56, 74, '0', 0, 15, gAnchorCenter, _layerBase);

    if (gLevelDetail[_curLevel].isClear)
        _iconClear = createImageWithFrameName("icon_medal.png", -90, 65, _layerBase);
    else
        _iconClear = createImageWithFrameName("icon_medal_d.png", -90, 65, _layerBase);

    createImageWithFrameName("icon_plate.png", -95, -55, _layerBase);    
    createLabel(getLocalizedString("Goal"), getLocalizedString("fontNormal"), 12, gAnchorCenter, ccWHITE, -95, -80, _layerBase);
    _labelGoal = createLabel(fcs("%d", gLevelDetail[_curLevel].goal), getLocalizedString("fontNormal"), 36, gAnchorLeft, ccYELLOW, -70, -60, _layerBase);

    createImageWithFrameName("icon_time.png", 30, -55, _layerBase);    
    createLabel(getLocalizedString("Time"), getLocalizedString("fontNormal"), 12, gAnchorCenter, ccWHITE, 30, -80, _layerBase);
    _labelTime = createLabel(fcs("%d", gLevelDetail[_curLevel].time), getLocalizedString("fontNormal"), 36, gAnchorLeft, ccYELLOW, 55, -60, _layerBase);
    
	CCMenu* menu = createMenu(_layerBase);
    
	CCMenuItem* item = createMenuItemWithCache("btn_close.png", "btn_close_d.png", 130, 105, kMenuClose, menu, this, menu_selector(DialogChellange::_menuCallback));
	createMenuItemWithCache("btn_left.png", "btn_left_d.png", -90, 15, kMenuLeft, menu, this, menu_selector(DialogChellange::_menuCallback));
	createMenuItemWithCache("btn_right.png", "btn_right_d.png", 90, 15, kMenuRight, menu, this, menu_selector(DialogChellange::_menuCallback));
	item = createMenuItemWithCache("btn_normal.png", "btn_normal_d.png", 0, -140, kMenuStart, menu, this, menu_selector(DialogChellange::_menuCallback));
    addMenuLabel(item, getLocalizedString("Start"), getLocalizedString("fontNormal"), 30, ccYELLOW);
    
	setTouchEnabled(true);
    
	_layerBase->runAction(CCMoveTo::create(0.3, ccpRatio(w2,330 - kStartPos)));
    
    return true;
}

void DialogChellange::_menuCallback(CCMenuItem* sender)
{
    MenuChellangeTag tag = (MenuChellangeTag)sender->getTag();
    
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    
    switch (tag) {
        case kMenuLeft:
            SimpleAudioEngine::sharedEngine()->playEffect("Menu.wav");
            _curLevel--;
            if (_curLevel < 0) _curLevel = 0;
            _labelCurLevel->setString(fcs("%d", _curLevel + 1));
            _labelGoal->setString(fcs("%d", gLevelDetail[_curLevel].goal));
            _labelTime->setString(fcs("%d", gLevelDetail[_curLevel].time));
            if(gLevelDetail[_curLevel].isClear)
                _iconClear->setDisplayFrame(cache->spriteFrameByName("icon_medal.png"));
            else
                _iconClear->setDisplayFrame(cache->spriteFrameByName("icon_medal_d.png"));
            break;
        case kMenuRight:
            SimpleAudioEngine::sharedEngine()->playEffect("Menu.wav");
            _curLevel ++;
            if (_curLevel > 9) _curLevel = 9;
            _labelCurLevel->setString(fcs("%d", _curLevel + 1));
            _labelGoal->setString(fcs("%d", gLevelDetail[_curLevel].goal));
            _labelTime->setString(fcs("%d", gLevelDetail[_curLevel].time));
            if(gLevelDetail[_curLevel].isClear)
                _iconClear->setDisplayFrame(cache->spriteFrameByName("icon_medal.png"));
            else
                _iconClear->setDisplayFrame(cache->spriteFrameByName("icon_medal_d.png"));
            break;
        case kMenuStart:
            HBUmeng::event("Button", "ChellangeStart");
            HBUmeng::event("LevelSelect", fcs("%d", _curLevel+1));
            SimpleAudioEngine::sharedEngine()->playEffect("upgrade.m4a");
            gGameMode = kGameModeTest;
            gTestLevel = _curLevel;
            DataSL::save();
            CCDirector::sharedDirector()->replaceScene(LayerMain::scene());
            break;
        case kMenuClose:
            HBUmeng::event("Button", "ChellangeClose");
            SimpleAudioEngine::sharedEngine()->playEffect("Menu.wav");
            _layerBase->runAction(CCSequence::create(CCMoveBy::create(0.3, ccpRatio(0,280)), CCCallFunc::create(this, callfunc_selector(DialogChellange::_closeDialog)), NULL));
            break;
    }
    //	SimpleAudioEngine::sharedEngine()->playEffect(EF_CLICK);
}

void DialogChellange::_closeDialog()
{
	removeFromParentAndCleanup(true);
}

void DialogChellange::onEnter()
{
	CCLayer::onEnter();
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true);    
}

void DialogChellange::onExit()
{
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}

bool DialogChellange::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	return true;
}